package cn.tedu.jdbchomework;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Day01LoginDemo {


    public static void main(String[] args) {
       while (true){
           System.out.println("请选择你需要的业务 1.登录 2.注册 3.退出");
           Scanner scan=new Scanner(System.in);
           int yw=scan.nextInt();
           scan.nextLine();
           if (yw==1){
               String[] user=scanMessage();
               try(Connection connection=DBUtil.getConnextion("tedu");) {
                   String sql="select username,password,nickname from userinfo where username=? and password=?";
                   PreparedStatement ps=connection.prepareStatement(sql);
                   ps.setString(1,user[0]);
                   ps.setString(2,user[1]);
                   ResultSet r=ps.executeQuery();
                   if (r.next()){
                       System.out.println("登录成功！ 欢迎您 "+r.getString("nickname"));
                       System.out.println();
                   }else{
                       System.out.println("登录失败！用户名或密码错误！");
                       System.out.println();
                   }

               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }else if (yw==2){
               String[] user=scanMessages();
               try(Connection connection=DBUtil.getConnextion("tedu");) {
                   String sql1="select username from userinfo where username=?";
                   PreparedStatement ps1=connection.prepareStatement(sql1);
                   ps1.setString(1,user[0]);
                   ResultSet r1=ps1.executeQuery();
                   if (r1.next()){
                       System.out.println("用户名已存在！请更换用户名");
                   }else{
                       String sql2="insert into userinfo(username,password,nickname,age) values (?,?,?,?)";
                       PreparedStatement ps2=connection.prepareStatement(sql2);
                       ps2.setString(1,user[0]);
                       ps2.setString(2,user[1]);
                       ps2.setString(3,user[2]);
                       ps2.setInt(4, Integer.parseInt(user[3]));
                       int i = ps2.executeUpdate();
                       if (i>0){
                           System.out.println("注册成功！");
                           System.out.println();
                       }else{
                           System.out.println("注册失败！");
                       }
                   }

               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }else{
               System.out.println("业务结束！");
               break;
           }
       }

    }
    public static String[] scanMessage(){
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入你的用户名~");
        String userName=scan.nextLine();
        System.out.println("请输入你的密码~");
        String passWord=scan.nextLine();
        return new String[] {userName,passWord};
    }
    public static String[] scanMessages(){
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入你的用户名~");
        String userName=scan.nextLine();
        System.out.println("请输入你的密码~");
        String passWord=scan.nextLine();
        System.out.println("请输入你的昵称~");
        String nickName=scan.nextLine();
        System.out.println("请输入你的年龄~");
        String age=scan.nextLine();
        return new String[] {userName,passWord,nickName,age};
    }
}
